//index.js

const app = getApp()
var landui = require("../../utils/util.js");
const innerAudioContext = wx.createInnerAudioContext();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    domain: app.globalData.trueDomain,
    banner: [],
    list_recommend: [],
    list_audio: [],
    list_video: [],
    list_activity: [],
    list_img: [],
    cid_video: 1,
    cid_special: 1,
    current_nav: 'index',
    option: 'pause',
    status: 'play'
  },

  // 播放音频
  playInnerAudio: function(e) {
    let that = this;
    let src = e.currentTarget.dataset.src;
    let idName = e.currentTarget.dataset.id_name;

    innerAudioContext.autoplay = true
    innerAudioContext.src = src
    innerAudioContext.obeyMuteSwitch = false
    wx.createInnerAudioContext().title = 123
    if (that.data.option == 'pause') {
      innerAudioContext.play();
      console.log('播放');
      that.setData({
        option: 'play'
      })
    } else {
      innerAudioContext.pause();
      console.log('暂停');
      that.setData({
        option: 'pause'
      })
    }
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },

  playAudio: function(e) {

    let that = this;
    console.log(e);
    let src = e.currentTarget.dataset.src;
    let idName = e.currentTarget.dataset.id_name;
    let AudioContext = wx.createAudioContext(idName)
    AudioContext.setSrc(src);
    if (that.data.option == 'play') {
      AudioContext.play();
      that.setData({
        option: 'pause'
      })
    } else {
      AudioContext.pause();
      that.setData({
        option: 'play'
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;

    landui.common.post({
      module: 'index',
      success: function(res) {
        console.log(res);
        res.data.recommend_article.map(item => {
          item.add_time = item.add_time.substring(0,10);
        });
        res.data.image_article.map(item => {
          item.add_time = item.add_time.substring(0,10);
        });
        res.data.special_article.map(item => {
          item.add_time = item.add_time.substring(0, 10);
        })

        that.setData({
          banner: res.data.banner,
          list_recommend: res.data.recommend_article,
          list_audio: res.data.audio,
          list_video: res.data.video,
          list_activity: res.data.special_article,
          list_img: res.data.image_article,
        })
      },
      fail: function() {
        
      }
    });

    //  that.audioObj = wx.createInnerAudioContext('#my-audio');
    // banner图  http://jk.hnla.cn/api/banner?number
    
    // landui.common.post({
    //   module: 'banner',
    //   success: function(res) {
    //     that.setData({
    //       banner: res.data,
    //     })
    //   },
    //   fail: function() {}
    // });


    // 首页今日推荐文章  http://jk.hnla.cn/api/getArticles?number=3 推荐的文章
    // landui.common.post({
    //   module: 'getArticles',
    //   param: {
    //     number: 5,
    //     isAids: 1
    //   },
    //   success: function(res) {
    //     var i = 0;
    //     that.list_recommend = res.data;
      
    //     res.data.map((item) => {
    //       item.add_time = item.add_time.substring(0, 10)
    //     })
    //     that.setData({
    //       list_recommend: res.data,
    //     })
    //   },
    //   fail: function() {}
    // });
    // 健康专访  http://jk.hnla.cn/api/getAudios?number=3&include=expert,expert.title,expert.department&isAids=1  推荐的音频
    //           http://jk.hnla.cn/api/audios?include=expert,expert.title,expert.department
    // isAids
    // landui.common.post({
    //   module: 'getAudios',
    //   param: {
    //     include: 'expert,expert.title,expert.department',
    //     isAids: 1,
    //     number: 2
    //   },
    //   success: function(res) {
    //     //console.log(res.data);
    //     that.setData({
    //       list_audio: res.data,
    //     })
    //   },
    //   fail: function() {

    //   }
    // });
    // 视频天地  http://jk.hnla.cn/api/getVideos?number=3  推荐的视频
    // landui.common.post({
    //   module: 'getVideos',
    //   param: {
    //     number: 4,
    //     isAids: 1
    //   },
    //   success: function(res) {
    //     that.setData({
    //       list_video: res.data,
    //     })
    //   },
    //   fail: function() {}
    // });
    // 图文库  https://ynjksh.cn/api/articles?page=1&number=6&type=image
    // landui.common.post({
    //   module: 'articles',
    //   param: {
    //     number: 3,
    //     page: 1,
    //     type: 'image'
    //   },
    //   success: function(res) {
    //     res.data.map((item) => {
    //       item.add_time = item.add_time.substring(0, 10);
    //     })
    //     that.setData({
    //       list_img: res.data,
    //     })
    //   },
    //   fail: function() {}
    // });
    // 活动专区  http://jk.hnla.cn/api/getArticles?number=3&classify=1
    //艾滋病栏目的id为3，就不用isAids参数了
    // landui.common.post({
    //   module: 'getArticles',
    //   param: {
    //     number: 3,
    //     classify: 3
    //   },
    //   success: function(res) {
    //     var i = 0;
    //     that.list_recommend = res.data;
    //     for (i; i < res.data.length; i++) {
    //       that.list_recommend[i]['add_time'] = res.data[i].add_time.substring(0, 10)
    //     }
    //     that.setData({
    //       list_activity: res.data,
    //     })
    //   },
    //   fail: function() {}
    // });
  },

  /**
   * 视频天地切换 
   */
  btnSwitch: function(e) {
    var that = this;
    var cid = e.target.dataset.cid
    that.setData({
      cid_video: cid
    })

    //视频防艾专区
    landui.common.post({
      module: 'getVideos',
      param: {
        number: 4,
        isAids: cid
      },
      success: function(res) {
        that.setData({
          list_video: res.data,
        })
      },
      fail: function() {}
    });
  },

  /**
   * 活动专区切换 
   */
  btnSpecialSwitch: function(e) {

    var that = this;
    var cid = e.currentTarget.dataset.cid_special
    that.setData({
      cid_special: cid
    })

    if (cid == 1) {
      // 防艾新闻取艾滋病栏目
      landui.common.post({
        module: 'getArticles',
        param: {
          number: 3,
          classify: 3
        },
        success: function(res) {

          var i = 0;
          that.list_recommend = res.data;
          for (i; i < res.data.length; i++) {
            that.list_recommend[i]['add_time'] = res.data[i].add_time.substring(0, 10)
          }
          that.setData({
            list_activity: res.data,
          })
        },
        fail: function() {}
      });
    } else {
      // 防艾活动取关于艾滋病的文章
      console.log('防艾新闻取艾滋病栏目');
      landui.common.post({
        module: 'getArticles',
        param: {
          number: 3,
          isAids: 1
        },
        success: function(res) {

          var i = 0;
          that.list_recommend = res.data;
          for (i; i < res.data.length; i++) {
            that.list_recommend[i]['add_time'] = res.data[i].add_time.substring(0, 10)
          }
          that.setData({
            list_activity: res.data,
          })
        },
        fail: function() {}
      });
    }
  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})